package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.SigInDao;
import com.swt.domain.Aboutcourse;
import com.swt.domain.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
@Repository(value = "TSigInDao")
public class SigInDaoImpl implements SigInDao {
    @Resource
    private HibernateTemplate hibernateTemplate;
//    查询当日的课程信息
    @Override
    public List<Course> sigInQuery(int courseClubid, String courseTime, int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Course where clubClubId = ? and courseClubid = ? and courseTime = ?";
        List<Course> courseList = session.createQuery(sql).setParameter(0,clubClubId).setParameter(1,courseClubid).setParameter(2,courseTime).list();


        return courseList;
    }
//查询指定课程下的预约人数
    @Override
    public List<Aboutcourse> sigInQueryDao(int courseId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        List<Aboutcourse> list = session.createQuery("from Aboutcourse where aboutcourseCourseid = ?").setParameter(0,courseId).list();

        return list;
    }
//修改签到状态
    @Override
    public boolean SigInUpdateStatus(int Id, String Status) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        if (Status.equals("待签到")){
            Status="已签到";
        }else {
            Status="待签到";
        }
        Aboutcourse aboutcourse = session.get(Aboutcourse.class,Id);
        aboutcourse.setAboutcourseStatus(Status);

        try {
            session.update(aboutcourse);
        }catch (Exception e){
            return false;
        }

        return true;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
